package data.join.com.carapi;

import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.WebSocket;
import com.koushikdutta.async.http.server.AsyncHttpServer;
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
import com.koushikdutta.async.http.server.HttpServerRequestCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import data.join.com.utils.MyLog;

/**
 * Created by Administrator on 2017/11/21.
 */

public class WebSocketServer {

    private static String TAG = WebSocketServer.class.getSimpleName();

    private static int SockPort = 17371;

    AsyncHttpServer server = new AsyncHttpServer();

    List<WebSocket> _sockets = new Vector<>();

    private static WebSocketServer webSocketServer;

    private WebSocketServer(){
//        server.get("/", new HttpServerRequestCallback() {
//            @Override
//            public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
//                response.send("Hello!!!");
//            }
//        });
        server = new AsyncHttpServer();
        server.listen(SockPort);
    }

    public static WebSocketServer getInstance(){
        if(webSocketServer == null){
            webSocketServer = new WebSocketServer();
        }
        return webSocketServer;
    }

    public void start() {
        server.websocket("/live", new AsyncHttpServer.WebSocketRequestCallback() {
            @Override
            public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {
                _sockets.add(webSocket);

                //Use this to clean up any references to your websocket
                webSocket.setClosedCallback(new CompletedCallback() {
                    @Override
                    public void onCompleted(Exception ex) {
                        try {
                            if (ex != null)
                                MyLog.E("WebSocket", "Error");
                        } finally {
                            _sockets.remove(webSocket);
                        }
                    }
                });

                webSocket.setStringCallback(new WebSocket.StringCallback() {
                    @Override
                    public void onStringAvailable(String s) {
                        if ("Hello Server".equals(s))
                            webSocket.send("Welcome Client!");
                    }
                });

            }
        });

//..Sometime later, broadcast!
//        new Thread() {
//            @Override
//            public void run() {
//                while (true) {
//
//                    try {
//                        Thread.sleep(10 * 1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//
//                    for (WebSocket socket : _sockets)
//                        socket.send("Fireball!");
//
//                    MyLog.D(TAG,"send msg Fireball");
//                }
//            }
//        }.start();
    }

    public void sendMassage(ApiMessage apiMessage){

        for (WebSocket socket : _sockets)
            socket.send(apiMessage.toString());
    }

}
